package cn.bugstack.infrastructure.whitelist;

import cn.bugstack.infrastructure.redis.IRedisService;
import cn.bugstack.types.common.Constants;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBitSet;
import org.redisson.api.RTopic;
import org.redisson.api.RedissonClient;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author mikael wang
 * @date 2025/9/17 20:21
 */
@Service
@Slf4j
public class WhiteListService {

    @Resource
    private IRedisService redisService;

    @Bean("whiteListTopic")
    public RTopic dccRedisTopicListener(RedissonClient redissonClient, RedissonClient redisson) {
        RTopic topic = redissonClient.getTopic("group_buy_market_dcc");
        topic.addListener(String.class, (charSequence, s) -> {
            String[] split = s.split(Constants.SPLIT);
            this.addIntoWhiteList(s);
        });
        return topic;
    }

    public boolean isInWhiteList(String userId) {
        RBitSet bitSet = redisService.getBitSet(Constants.WHITE_LIST);
        if (!bitSet.isExists()) {
            return false;
        }
        return bitSet.get(redisService.getIndexFromUserId(userId));
    }

    public void addIntoWhiteList(String userId) {
        RBitSet bitSet = redisService.getBitSet(Constants.WHITE_LIST);
        if (!bitSet.isExists()) {
        }
        bitSet.set(redisService.getIndexFromUserId(userId));

    }
}
